package com.example.controller.admin;

import com.example.constant.JwtClaimsConstant;
import com.example.dto.UserByStorePageQueryDTO;
import com.example.dto.UserDTO;
import com.example.dto.UserLoginDTO;
import com.example.dto.UserPageQueryDTO;
import com.example.entity.User;
import com.example.properties.JwtProperties;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.UserService;
import com.example.utils.JwtUtil;
import com.example.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1. @ClassDescription:
 * 2. @author: Z
 * 3. @date: 2024年03月20日 15:30
 */
@RestController
@RequestMapping("/admin/user")
@Slf4j
@Api(tags = "管理页面用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("员工登录：{}", userLoginDTO);

        Result<User> result= userService.login(userLoginDTO);
        User user = result.getData();

        if(user == null){
            return Result.error(result.getMsg());
        }

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        //存用户id和商店id
        claims.put(JwtClaimsConstant.EMP_ID, user.getId());
        claims.put(JwtClaimsConstant.STO_ID,user.getStoreId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .roleId(user.getRoleId())
                .username(user.getUsername())
                .name(user.getName())
                .token(token)
                .storeId(user.getStoreId())
                .avatar(user.getAvatar())
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工登出")
    public Result<String> logout() {
        return Result.success();
    }


    @PostMapping
    @ApiOperation("新增用户")
    public Result<User> save(@RequestBody UserDTO userDTO){
        log.info("新增员工:{}",userDTO);
        User user = userService.save(userDTO);
        if(user!=null)
            return Result.success(user);
        else
            return Result.error("添加失败，用户名重复");
    }

    /**
     * @description: 员工分页查询
     * @param userPageQueryDTO
     * @return:
     * @author: 18360
     * @time: 2023/11/30 20:35
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(UserPageQueryDTO userPageQueryDTO){
        log.info("员工分页查询，参数为：{}",userPageQueryDTO);
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * @description: 启用禁用员工账号
     * @param status
     * @param id
     * @return: com.sky.result.Result
     * @author: 18360
     * @time: 2023/11/30 21:22
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启用禁用员工账号：{},{}",status,id);
        userService.startOrStop(status,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<User> getById(@PathVariable Long id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result<User> update(@RequestBody UserDTO userDTO){
        log.info("编辑员工信息:{}",userDTO);
        User user = userService.update(userDTO);
        if(user == null){
            return Result.error("用户名重复");
        }else
            return Result.success(user);
    }



    @GetMapping("/queryUserByStoreId")
    @ApiOperation("根据商店id查询员工")
    public Result<List<User>> queryUserByStoreId(Long storeId){
        log.info("商店id:{}",storeId);
        Result<List<User>> result = userService.queryUserByStoreId(storeId);
        return result;
    }

    @PutMapping("/updatePassword")
    @ApiOperation("修改密码")
    public Result updatePassword(Long id, String oldPassword,String newPassword){
        log.info("id,新密码,旧密码:{},{},{}",id,oldPassword,newPassword);
        Result result = userService.updatePassword(id,oldPassword,newPassword);
        return result;
    }

}
